<?php
class XUser {
	/**
	 * @var string
	 */
	public $account;
	/**
	 * @var integer
	 */
	public $status;
	/**
	 * @var array
	 */
	public $attribute;
	
	const IDENT_GUEST = 0;
	const IDENT_USER  = 1;
	
	const STATUS_OFFLINE = 0;
	const STATUS_ONLINE = 1;
	
	/**
	 * 
	 */
	function __construct(){
		$this->account = '';
		$this->status = self::STATUS_OFFLINE;
		$this->info = array();
		
		if ( isset($_SESSION['X']['User']['Account']) ){
			$this->account = $_SESSION['X']['User']['Account'];
		}
		
		if ( isset($_SESSION['X']['User']['Status']) ){
			$this->status  = $_SESSION['X']['User']['Status'];
		}
		
		if ( isset($_SESSION['X']['User']['Attribute']) ){
			$this->info = $_SESSION['X']['User']['Attribute'];
		}
	}
	
	/**
	 * @return integer
	 */
	public function getIdent(){
		if ( !empty($this->account) && self::STATUS_ONLINE == $this->status ){
			return self::IDENT_USER;
		}
		else {
			return self::IDENT_GUEST;
		}
	}
	
	/**
	 * @param string $account
	 * @return XUser
	 */
	public function setAccount($account){
		$this->account = $account;
		
		return $this;
	}
	
	/**
	 * @param integer $status
	 * @return XUser
	 */
	public function setStatus($status){
		if ( $status != self::STATUS_OFFLINE && $status != self::STATUS_ONLINE ) {
			$this->status = self::STATUS_OFFLINE;
		}
		else { 
			$this->status = $status;
		}
		
		return $this;
	}
	
	/**
	 * @param string $attr
	 * @param mixed $value
	 * @return XUser
	 */
	public function setAttribute($attr, $value){
		$this->attribute[$attr] = $value;
		
		$_SESSION['X']['User']['Attribute'][$attr] = $value;
		
		return $this;
	}
}